package br.uefs.ecomp.myBook.view;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.*;

import javax.swing.*;

import br.uefs.ecomp.myBook.util.ImageOperations;

public class GenericLink extends JLabel implements MouseListener {

	private ActionListener listener;
	private Color backgroundColor = new Color(0xff,0xff,0xff);
	private Color backgroundHoverColor = new Color(0xcc, 0xcc, 0xcc);
	private String actionCommand;
	
	public GenericLink(String text, ActionListener listener, String command){
		super(text, JLabel.CENTER);
		initConfig(listener, command);
	}
	
	public GenericLink(Icon icon, ActionListener listener, String command){
		super(icon, JLabel.CENTER);
		initConfig(listener, command);
	}
	
	public GenericLink(String text, ActionListener listener, String command, Color background, 
			Color backgroundHover, Dimension dimension){
		
		super(text, JLabel.CENTER);
		this.backgroundColor = background;
		this.backgroundHoverColor = backgroundHover;
		
		setPreferredSize(dimension);
		initConfig(listener, command);
	}
	
	public GenericLink(Icon icon, ActionListener listener, String command, Color background, 
			Color backgroundHover, Dimension dimension){
		
		super(icon, JLabel.CENTER);
		this.backgroundColor = background;
		this.backgroundHoverColor = backgroundHover;
		initConfig(listener, command);
		
		setPreferredSize(dimension);
		setIcon(ImageOperations.scaleIcon(icon, dimension));
		repaint();
		revalidate();
	}
	
	private void initConfig(ActionListener listener, String command){
		this.listener = listener;
		this.actionCommand = command;
		
		setOpaque(true);
		setCursor(new Cursor(Cursor.HAND_CURSOR));
		setBackground(backgroundColor);
		addMouseListener(this);
	}
	
	@Override
	public void mouseReleased(MouseEvent e) {
		listener.actionPerformed(new ActionEvent(this, 0, actionCommand));
	}

	@Override
	public void mouseEntered(MouseEvent e) {
		setBackground(backgroundHoverColor);
	}

	@Override
	public void mouseExited(MouseEvent e) {
		setBackground(backgroundColor);
	}
	
	@Override
	public void mouseClicked(MouseEvent e) {}

	@Override
	public void mousePressed(MouseEvent e) {}

}
